/*
 * Copyright QLH 2023
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.feeyo.groundservice.composewidget.view.value_selector.date_selector

import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.feeyo.groundservice.composewidget.view.value_selector.ValueSelectState

/**
 * creator: QLH
 *
 * effect : DateSelector的状态
 *          State of the [DateSelector]
 *
 * warning:
 *
 * @param defaultYear 默认选中的日期   Default selected date
 * @param defaultMonth 默认选中的月份  Default selected month
 * @param defaultDay 默认选中的天  Default selected day
 *
 * @param isShowYear 是否显示年份  Whether to show the year
 * @param isShowMonth 是否显示月份  Whether to show the month
 * @param isShowDay 是否显示天  Whether to show the day
 *
 * @param minYear 最小可选择日期  Minimum selected date
 * @param maxYear 最大可选择日期  Maximum selected date
 *
 */
@Stable
class DateSelectorState(
    internal val defaultYear: Int,
    internal val defaultMonth: Int,
    internal val defaultDay: Int,
    private val isShowYear: Boolean = true,
    private val isShowMonth: Boolean = true,
    private val isShowDay: Boolean = true,
    private val minYear: Int = 1900,
    private val maxYear: Int = 2100,
) {
    val yearState = ValueSelectState().apply { isShow = isShowYear }
    val monthState = ValueSelectState().apply { isShow = isShowMonth }
    val dayState = ValueSelectState().apply { isShow = isShowDay }

    internal val years by mutableStateOf(ArrayList((minYear..maxYear).map { it.toString() }))
    internal val months by mutableStateOf(ArrayList((1..12).map { it.toString() }))
    internal var days by mutableStateOf(ArrayList((1..31).map { it.toString() }))

    fun getYear(): String = if (yearState.isShow) years[yearState.getSelectIndex()] else ""
    fun getMonth(): String = if (yearState.isShow) months[monthState.getSelectIndex()].let {
        if (it.length == 1) "0$it" else it
    } else ""

    fun getDay(): String = if (dayState.isShow) days[dayState.getSelectIndex()].let {
        if (it.length == 1) "0$it" else it
    } else ""
}